#!/bin/sh
#
# Copyright (C) 2000-2025 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a simple backup of the Bacula build directory
# and test the cancel all command
#
# We test "cancel all" => no/. nothing canceled, restore possible
# We test "cancel all" => yes and "cancel all yes", two canceled jobs
#
TestName="cancel-all-test"
JobName=backup
. scripts/functions

scripts/cleanup
scripts/copy-confs

#
# Zap out any schedule in default conf file so that
#  it doesn't start during our test
#
outf="$tmp/sed_tmp"
echo "s%  Schedule =%# Schedule =%g" >${outf}
cp $scripts/bacula-dir.conf $tmp/1
sed -f ${outf} $tmp/1 >$scripts/bacula-dir.conf

change_jobname BackupClient1 $JobName
start_test

cat <<END_OF_DATA >$tmp/bconcmds
@output /dev/null
messages
@$out $tmp/log1.out
label volume=TestVolume001 storage=File1 pool=File slot=1 drive=0
setbandwidth limit=2MB/s client
run job=$JobName yes
@sleep 2
cancel all
no
cancel all
.
setbandwidth limit=100MB/s client
wait
messages
@# 
@# now do a restore
@#
@$out $tmp/log2.out  
setdebug level=4 storage=File1
restore where=$tmp/bacula-restores select all done
yes
wait
messages
@$out $tmp/log3.out
setbandwidth limit=2MB/s client
run job=$JobName level=full yes
@sleep 2
cancel all
yes
setbandwidth limit=100MB/s client
wait
messages
@$out $tmp/log3.out
setbandwidth limit=2MB/s client
run job=$JobName level=full yes
@sleep 2
cancel all yes
setbandwidth limit=100MB/s client
wait
messages
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=File1
stop_bacula

nb=`grep "Backup Canceled" $tmp/log3.out | wc -l`
if [ $nb != 2 ]; then
    print_debug "ERROR: Should find two jobs canceled in $tmp/log3.out"
    estat=1
fi

check_two_logs
check_restore_diff
end_test
